﻿@using Masa.BuildingBlocks.StackSdks.Config;
@using Masa.Contrib.StackSdks.Config;
@inherits LayoutComponentBase
@inject GlobalConfig GlobalConfig
@inject NavigationManager Navigation
@inject IMasaStackConfig MasaStackConfig

<SLayout OnSignOut="SignOut" OnErrorAsync="OnError" IsShowEnvironmentSwitch WhiteUris="WhiteUris"
         Logo="https://cdn.masastack.com/stack/images/logo/MASAStack/logo-h-en.png"
         MiniLogo="https://cdn.masastack.com/stack/images/logo/MASAStack/logo.png">
    @Body
    <MLoading />
</SLayout>

@code {
    [Inject]
    public IPopupService PopupService { get; set; } = default!;

    [Inject]
    public I18n I18N { get; set; } = default!;

    private List<string> WhiteUris = new List<string>() { "/channels/channelManagement", "/messageTasks/sendMessage" };

    private Task OnError(Exception exception)
    {
        PopupService.EnqueueSnackbarAsync(exception.Message, AlertTypes.Error);
        GlobalConfig.Loading = false;
        GlobalConfig.ThrottleFlag = false;
        return Task.CompletedTask;
    }

    private bool SignOut()
    {
        Navigation.NavigateTo("/Logout",true);
        return true;
    }

    protected bool TranslateFluentValidationProperty { get; set; } = true;

    private void UsingI18nToResolveFluentValidationDisplayName(I18n i18N)
    {
        ValidatorOptions.Global.DisplayNameResolver = (type, member, expression) =>
        {
            if (member != null)
            {
                var memberName = i18N.T(member.Name);
                return memberName;
            }
            return null;
        };
    }

    protected override Task OnAfterRenderAsync(bool firstRender)
    {
        if (TranslateFluentValidationProperty)
        {
            UsingI18nToResolveFluentValidationDisplayName(I18N);
        }
        return base.OnAfterRenderAsync(firstRender);
    }
}